<?php

namespace services\patient;

use Yii;
use common\components\Service;
use common\helpers\CommonHelper;
use common\models\patient\MedicalRecords;
use api\modules\v1\forms\patient\MedicalForm;

/**
 * Class MedicalRecordsService
 * @package services\patient
 */
class MedicalRecordsService extends Service
{
    /**
     * 根据ID查找信息
     *
     * @param int $id
     * @return MedicalRecords|null
     */
    public function findById(int $id = 0): ?MedicalRecords
    {
        return MedicalRecords::find()->where(['id' => $id])->one();
    }

    /**
     * 查找信息
     *
     * @param array $condition
     * @return MedicalRecords|null
     */
    public function findInfo(array $condition = []): ?MedicalRecords
    {
        $query = MedicalRecords::find();

        isset($condition['project_id']) && $query->andWhere(['project_id' => $condition['project_id']]);
        isset($condition['patient_user_id']) && $query->andWhere(['patient_user_id' => $condition['patient_user_id']]);

        return $query->one();
    }

    /**
     * 查找列表
     *
     * @param array $condition
     * @return array|\yii\db\ActiveRecord[]
     */
    public function findList(array $condition = [])
    {
        $query = MedicalRecords::find();

        isset($condition['patient_user_id']) && $query->andWhere(['patient_user_id' => $condition['patient_user_id']]);
        isset($condition['body_parts_id']) && $query->andWhere(['body_parts_id' => $condition['body_parts_id']]);
        isset($condition['status']) && $query->andWhere(['status' => $condition['status']]);

        return $query->asArray()->all();
    }

    /**
     * 编辑病历
     *
     * @param MedicalForm $model
     * @return array|mixed
     */
    public function edit(MedicalForm $model)
    {
        try {
            // 病种信息
            $patientDiseases = $model->getPatientDiseases();

            // 1.更新病历
            $medicalRecord = $model->getMedical();
            if (!$medicalRecord) {
                $medicalRecord = new MedicalRecords();
                $medicalRecord->project_id = $model->project_id;
                $medicalRecord->patient_user_id = $model->patient_user_id;
            }
            $medicalRecord->sick_time = $model->sick_time ? strtotime($model->sick_time) : 0;
            $medicalRecord->main_desc = $model->main_desc;
            $medicalRecord->now_medical_history = $model->now_medical_history;
            $medicalRecord->allergy_history = $model->allergy_history;
            $medicalRecord->jw_history = $model->jw_history;
            $medicalRecord->person_history = $model->person_history;
            $medicalRecord->family_history = $model->family_history;
            $medicalRecord->physique_history = $model->physique_history;
            $medicalRecord->diagnose = $model->diagnose;
            $medicalRecord->auxiliary_result = $model->auxiliary_result;
            $medicalRecord->clinic_opinion = $model->clinic_opinion;
            $medicalRecord->patient_diseases_id = $patientDiseases ? $model->patient_diseases_id : 0;
            $medicalRecord->diseases_name = $patientDiseases ? $patientDiseases->diseases_name : '';
            $medicalRecord->operate_id = Yii::$app->user->isGuest ? 0 : Yii::$app->user->identity->member_id;
            $medicalRecord->save();

            return ['id' => $medicalRecord->id];
        } catch (\Exception $e) {
            return CommonHelper::api(422, $e->getMessage());
        }
    }
}
